import {Component, OnDestroy, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {NzMessageService} from "ng-zorro-antd/message";
import {NzModalService} from "ng-zorro-antd/modal";
import {ConfigService} from "../../../shared/services/config.service";
import {CustomerService} from "../../../shared/services/product/customer.service";
import * as moment from 'moment';

@Component({
    selector: 'app-customer-detail',
    templateUrl: './customer-detail.component.html',
    styleUrls: ['./customer-detail.component.scss']
})
export class CustomerDetailComponent implements OnInit, OnDestroy {

    source: any;
    object: any;

    _subscriptions = [];

    option: any = {
        sell: {
            range: [],
            current: 1,
            fields: ['code'],
            keyword: '',
            paid: 'all'
        }
    };

    sell_page: any = [];
    sells: any = [];
    _sells: any = [];

    constructor(private router: Router, private route: ActivatedRoute, private customerService: CustomerService, public configService: ConfigService,
                private modal: NzModalService, private message: NzMessageService) {
    }

    ngOnInit() {
        this._subscriptions.push(this.route.data.subscribe(res => {
            this.source = res.data;
            //this.object = JSON.parse(JSON.stringify(this.source));
            this.object = this.source.clone();
        }));
    }

    ngOnDestroy(): void {
        this._subscriptions.forEach( sub => {
            sub.unsubscribe();
        });
    }

    cancel(){
        //this.object = JSON.parse(JSON.stringify(this.source));
        this.object = this.source.clone();
        this.router.navigate(['list'], {relativeTo: this.route.parent});
    }

    save(){
        this.customerService.update(this.object)
            .subscribe(res => {
                if (res.result) {
                    this.message.create('success', '保存成功！');
                    this.customerService.emit('update', this.object);
                    this.router.navigate(['list'], {relativeTo: this.route.parent});
                } else {
                    this.message.create('warning', '名称已存在！');
                }
            });
    }

    delete(){
        this.modal.confirm({
            nzTitle: '提示！',
            nzContent: `是否确认删除？`,
            nzOkText: '确定',
            nzOkType: 'primary',
            nzOnOk: () => {
                this.customerService.delete(this.object._id)
                    .subscribe(res => {
                        if (res.result){
                            this.message.create('success', '删除成功！');
                            this.customerService.emit('delete', this.object);
                            this.router.navigate(['list'], {relativeTo: this.route.parent});
                        } else {
                            this.message.create('warning', '删除失败！');
                        }
                    });
            }
        });
    }

}
